var L=["L10","L20","L30","L40","L50","L60","L70","L80","L90","L100"];
var LI=["LI10","LI20","LI30","LI40","LI50","LI60","LI70","LI80","LI90","LI100"];
var R=["R10","R20","R30","R40","R50","R60","R70","R80","R90","R100"];
var RI=["RI10","RI20","RI30","RI40","RI50","RI60","RI70","RI80","RI90","RI100"];
var DL=["L01","L02","L03","L04","L05","L06","L07","L08"];
var DR=["R01","R02","R03","R04","R05","R06","R07","R08"];
var exploration=true; 
var round = 1;
var ranR=0;
var ranL=0;
var block=1;
var newTrial=false;
var roundNum=1;



//introduction
var int = 1;
var intR = 0;
var intL = 0;
var ranRI=0;
var ranLI=0;
var topHead = document.getElementById("roundNumber");
var titHead= document.getElementById("titleHead");
var intHead = document.getElementById("topInstructions");
var intBottom = document.getElementById("intBottom");
var BButton=document.getElementById("bottomButton");
var expInt=true;

titHead.style.display="none";
topHead.innerHTML = 'CARD TASK - INSTRUCTIONS';
intHead.innerHTML = 'The following card task contains 16 rounds.<br>In each round you will be presented with two decks of cards similar to these two:';
intBottom.innerHTML="Each deck contains cards with different values up to 100¢. In order to draw a card from any deck - simply click on the deck. <br><span>Try it!</span> <br><strong>Click a few times on each deck.</strong>";
document.getElementById("LInt").className = "";
document.getElementById("LInt").setAttribute("onclick","drawFromDeckLPractice()");
document.getElementById("RInt").className = "";
document.getElementById("RInt").setAttribute("onclick","drawFromDeckRPractice()");
BButton.style.display="none";

function int2() {
int++;
intHead.innerHTML="";
intBottom.innerHTML='Great! As you see: After drawing a card, it is returned to the deck in a random fashion.<br>Click on the "continue" button to continue.';
document.getElementById("bottomButton").setAttribute("onClick","int3()");    
}

function int3() {
int++;
intBottom.innerHTML='During each round you may explore both decks as much as you would like.<br>When you are ready - You may draw <u>one</u> final card from <u>one</u> of the deck. This card will be a card that counts and will effect the amount of bonus you will receive!<br>Click on the "continue" button to continue.';
document.getElementById("bottomButton").setAttribute("onClick","int4()");    
}

function int4() {
int++;
intBottom.innerHTML="Go a head and explore the decks some more.<br>When you feel ready click on the button bellow.";
document.getElementById("bottomButton").setAttribute("onClick","int5()");
document.getElementById("bottomButton").innerHTML="I&#39;m ready to draw a final card";
document.getElementById("bottomButton").style.width = "300px"; 
document.getElementById("bottomButton").style.background = "gray";  
}

function int5(){
expInt=false;
intBottom.innerHTML="Go a head and draw a card a final card!";
document.getElementById("bottomButton").style.display="none";
}

function int6() {
intHead.style.textAlign="left";
titHead.style.display="";
titHead.innerHTML="Number of rounds";
intHead.innerHTML="During this experiment you will participate in 16 rounds. <u>The decks won't be the same in each round.</u>";
document.getElementById("LInt").style.visibility="";
document.getElementById("LInt").className = "Left";
document.getElementById(RI[ranRI]).className = "Right";
document.getElementById("RInt").style.visibility="";
document.getElementById("RInt").className = "Right";
document.getElementById(LI[ranLI]).className = "Left";
document.getElementById("bottomButton").style.display="";
document.getElementById("bottomButton").setAttribute("onClick","int7()");
}

function int7() {
titHead.innerHTML="Final cards";
intHead.innerHTML="In each round you will be given an opportunity to explore the decks. Each round will conclude with you drawing a final card that will effect the amount of bonus you will receive.";
document.getElementById("LInt").style.visibility="";
document.getElementById("LInt").className = "Left";
document.getElementById(RI[ranRI]).className = "Right";
document.getElementById("RInt").style.visibility="";
document.getElementById("RInt").className = "Right";
document.getElementById(LI[ranLI]).className = "Left";
document.getElementById("bottomButton").style.display="";
document.getElementById("bottomButton").setAttribute("onClick","int8()");
}

function int8() {
titHead.innerHTML="Bonus payment";
intHead.innerHTML="Your bonus will be determined as followed: At the end of the experiment one of the 16 final cards will be randomly chosen, and you will receive the amount on that card. In each round: <u>The higher the final cards you draw the higher the chances are that you will receive a higher bonus!<u>";
document.getElementById("LInt").style.visibility="";
document.getElementById("LInt").className = "Left";
document.getElementById(RI[ranRI]).className = "Right";
document.getElementById("RInt").style.visibility="";
document.getElementById("RInt").className = "Right";
document.getElementById(LI[ranLI]).className = "Left";
document.getElementById("bottomButton").style.display="";
document.getElementById("bottomButton").setAttribute("onClick","startExperiment()");
introduction=false;
}


function drawFromDeckLPractice(){
if (expInt){
intL++;
ranLI = Math.floor(Math.random()*10);
document.getElementById("LInt").removeAttribute("onclick");
document.getElementById("LInt").className = "Left";
document.getElementById(LI[ranLI]).className = "";
setTimeout(function(){document.getElementById(LI[ranLI]).className = "Left";}, 1000);
setTimeout(function(){document.getElementById("LInt").className = "";}, 1000);
setTimeout(function(){document.getElementById("LInt").setAttribute("onclick","drawFromDeckLPractice()");}, 1000);
resultString =resultString+LI[ranLI];
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
if(intR+intL>5 && int===1){
document.getElementById("bottomButton").style.display="";
document.getElementById("bottomButton").innerHTML= "continue";
document.getElementById("bottomButton").style.background = "#a62500";
document.getElementById("bottomButton").style.fontSize="16px";
document.getElementById("bottomButton").setAttribute("onClick","int2()");
}
}
else{
ranL = Math.floor(Math.random()*10);
document.getElementById("RInt").removeAttribute("onclick");
document.getElementById("LInt").removeAttribute("onclick");
document.getElementById("LInt").className = "Left";
document.getElementById("RInt").style.visibility="hidden";
document.getElementById(LI[ranLI]).className = "";
resultString =resultString+LI[ranLI];
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
intHead.innerHTML="This is an example of a final card.<br> final cards you collect at the end of each round<br>will effect the amount of bonus you will receive<br>at the end the experiment.";
intBottom.innerHTML="";
document.getElementById("bottomButton").style.display = "";
document.getElementById("bottomButton").innerHTML= "continue";
document.getElementById("bottomButton").style.background = "#a62500";
document.getElementById("bottomButton").style.fontSize="16px";
document.getElementById("bottomButton").setAttribute("onClick","int6()");
}
}

function drawFromDeckRPractice(){
if (expInt){
intR++;
ranRI = Math.floor(Math.random()*10);
document.getElementById("RInt").removeAttribute("onclick");
document.getElementById("RInt").className = "Right";
document.getElementById(RI[ranRI]).className = "";
setTimeout(function(){document.getElementById(RI[ranRI]).className = "Right";}, 1000);
setTimeout(function(){document.getElementById("RInt").className = "";}, 1000);
setTimeout(function(){document.getElementById("RInt").setAttribute("onclick","drawFromDeckRPractice()");}, 1000);
resultString =resultString+RI[ranRI];
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
if (intR >2 && intL>2 && int===1){
document.getElementById("bottomButtonInt").style.display="";
document.getElementById("bottomButtonInt").innerHTML= "continue";
document.getElementById("bottomButtonInt").style.background = "#a62500";
document.getElementById("bottomButtonInt").style.fontSize="16px";
document.getElementById("bottomButtonInt").setAttribute("onClick","int2()");
}
}
else{
ranR = Math.floor(Math.random()*10);
document.getElementById("RInt").removeAttribute("onclick");
document.getElementById("LInt").removeAttribute("onclick");
document.getElementById("RInt").className = "Right";
document.getElementById("LInt").style.visibility="hidden";
document.getElementById(RI[ranRI]).className = "";
resultString =resultString+RI[ranRI];
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
intHead.innerHTML="This is an example of a final card.<br> final cards you collect at the end of each round<br>will effect the amount of bonus you will receive<br>at the end the experiment.";
intBottom.innerHTML="";
document.getElementById("bottomButton").innerHTML= "continue";
document.getElementById("bottomButton").style.display = "";
document.getElementById("bottomButton").style.background = "#a62500";
document.getElementById("bottomButton").style.fontSize="16px";
document.getElementById("bottomButton").setAttribute("onClick","int6()");
}
}



//experiment!

//randomize deck expectancy
var x=Math.floor(Math.random()*2)
if(x){
var leftE=false;
var rightE=true;
}
else {
var leftE=true;
var rightE=false;
}

//randomize trial
var trialOrder=[1,2,3,4];
var ranTrial=Math.floor(Math.random()*4);
var trial=trialOrder[ranTrial];
trialOrder.splice(ranTrial,1);
var resultString ="B"+block+"T"+trial;

//randomize deck color
var randoms=[0,1,2,3,4,5,6,7];
var ranNumber=Math.floor(Math.random()*8);
var ranDL=randoms[ranNumber];
randoms.splice(ranNumber,1);
var ranNumber=Math.floor(Math.random()*7)
var ranDR=randoms[ranNumber];
randoms.splice(ranNumber,1);
resultString = resultString+ranDL+ranDR;
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);


function startExperiment(){
titHead.style.display="none";
document.getElementById("LI").className = "";
document.getElementById("RI").className = "";
document.getElementById(DR[ranDR]).className = "";
document.getElementById(DL[ranDL]).className = "";
document.getElementById(DR[ranDR]).setAttribute("onclick","drawFromDeckR()");
document.getElementById(DL[ranDL]).setAttribute("onclick","drawFromDeckL()");
document.getElementById("bottomButton").setAttribute("onclick","finalDraw()");
document.getElementById("bottomButton").style.display="";
document.getElementById("bottomButton").innerHTML="I&#39;m ready to draw a final card";
document.getElementById("bottomButton").style.width = "300px"; 
document.getElementById("bottomButton").style.background = "gray";
document.getElementById("roundNumber").innerHTML = 'Round Number'+roundNum;
document.getElementById("topInstructions").innerHTML = 'click on either of the decks to draw a card'; 
}








function drawFromDeckL(){
if (trial===1){
	L=["L10","L20","L30","L40","L50","L60","L70","L80","L90","L100"];
	ranL = Math.floor(Math.random()*10);
}
else if (trial===2){
	if (leftE){
	L=["L10","L20","L30","L40","L50","L60","L70","L80"];
	}
	else {
	L=["L30","L40","L50","L60","L70","L80","L90","L100"];
	}
	ranL = Math.floor(Math.random()*8);
} 
else if (trial===3){
	if (leftE){
	L=["L10","L20","L30","L40","L50","L60"];
	}
	else {
	L=["L50","L60","L70","L80","L90","L100"];
	}
	ranL = Math.floor(Math.random()*6);
}
else {
	if (leftE){
	L=["L60"];
	}
	else {
	L=["L50"];
	}
	ranL = 0;
}
if(exploration){
document.getElementById(DL[ranDL]).removeAttribute("onclick");
document.getElementById(DL[ranDL]).className = "Left";
document.getElementById(L[ranL]).className = "";
setTimeout(function(){document.getElementById(L[ranL]).className = "Left";}, 1000);
setTimeout(function(){document.getElementById(DL[ranDL]).className = "";}, 1000);
setTimeout(function(){document.getElementById(DL[ranDL]).setAttribute("onclick","drawFromDeckL()");}, 1000);
resultString =resultString+L[ranL];
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
}
else{
document.getElementById(DL[ranDL]).removeAttribute("onclick");
document.getElementById(DR[ranDR]).removeAttribute("onclick");
document.getElementById(DL[ranDL]).className = "Left";
document.getElementById(L[ranL]).className = "";
document.getElementById(DR[ranDR]).style.visibility="hidden";
resultString =resultString+L[ranL];
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
document.getElementById("topInstructions").innerHTML = 'press the "continue" button to proceed to another round';
setTimeout(function(){document.getElementById("bottomButton").style.display="";},1000);
document.getElementById("bottomButton").innerHTML= "Continue to next round";
document.getElementById("bottomButton").style.background = "red";
document.getElementById("bottomButton").setAttribute("onClick","newRound()");
}
}
function drawFromDeckR(){

if (trial===1){
	R=["R10","R20","R30","R40","R50","R60","R70","R80","R90","R100"];
	ranR = Math.floor(Math.random()*10);
}
else if (trial===2){
	if (rightE){
	R=["R10","R20","R30","R40","R50","R60","R70","R80"];
	}
	else {
	R=["R30","R40","R50","R60","R70","R80","R90","R100"];
	}
	ranR = Math.floor(Math.random()*8);
}
else if (trial===3){
	if (rightE){
	R=["R10","R20","R30","R40","R50","R60"];
	}
	else {
	R=["R50","R60","R70","R80","R90","R100"];
	}
	ranR = Math.floor(Math.random()*6);
}
else {
	if (rightE){
	R=["R60"];
	}
	else {
	R=["R50"];
	}
	ranR = 0;
}	

if (exploration){
document.getElementById(DR[ranDR]).removeAttribute("onclick");
document.getElementById(DR[ranDR]).className = "Right";
document.getElementById(R[ranR]).className = "";
setTimeout(function(){document.getElementById(R[ranR]).className = "Right";}, 1000);
setTimeout(function(){document.getElementById(DR[ranDR]).className = "";}, 1000);
setTimeout(function(){document.getElementById(DR[ranDR]).setAttribute("onclick","drawFromDeckR()");}, 1000);
resultString =resultString+R[ranR];
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
}
else{
document.getElementById(DR[ranDR]).removeAttribute("onclick");
document.getElementById(DL[ranDL]).removeAttribute("onclick");
document.getElementById(DR[ranDR]).className = "Right";
document.getElementById(DL[ranDL]).style.visibility="hidden";
document.getElementById(R[ranR]).className = "";
resultString =resultString+R[ranR];
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
document.getElementById("topInstructions").innerHTML = 'press the "continue" button to proceed to another round';
setTimeout(function(){document.getElementById("bottomButton").style.display="";},1000);
document.getElementById("bottomButton").innerHTML="Continue to next round";
document.getElementById("bottomButton").style.background = "red";
document.getElementById("bottomButton").setAttribute("onclick","newRound()");
}
}

function finalDraw(){
exploration=false;
document.getElementById("topInstructions").innerHTML = "Ok, this is a draw that counts!";
document.getElementById("bottomButton").style.display="none";
}



function newRound() {
if(block<3){
exploration=true;

roundNum++;
document.getElementById("roundNumber").innerHTML = "Round Number "+roundNum;
document.getElementById("topInstructions").innerHTML = "New Round: Click on on either of the decks in order to draw a card";
document.getElementById(R[ranR]).className = "Right";
document.getElementById(L[ranL]).className = "Left";
document.getElementById("bottomButton").innerHTML="I&#39;m ready to draw a card that counts";
document.getElementById("bottomButton").style.background = "gray";
document.getElementById("bottomButton").setAttribute("onclick","finalDraw()");

x=Math.floor(Math.random()*2)
if(x){
leftE=false;
rightE=true;
}
else {
leftE=true;
rightE=false;
}

document.getElementById(DL[ranDL]).style.visibility="";
document.getElementById(DR[ranDR]).style.visibility="";
document.getElementById(DL[ranDL]).className="Left";
document.getElementById(DR[ranDR]).className="Right";


if(trialOrder.length===4){

ranTrial=Math.floor(Math.random()*4);
trial=trialOrder[ranTrial];
trialOrder.splice(ranTrial,1);

randoms=[0,1,2,3,4,5,6,7];
ranNumber=Math.floor(Math.random()*8);
ranDL=randoms[ranNumber];
randoms.splice(ranNumber,1);
ranNumber=Math.floor(Math.random()*7);
ranDR=randoms[ranNumber];
randoms.splice(ranNumber,1);

resultString =resultString+"B"+block+"T"+trial+ranDL+ranDR;
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
} 

else if (trialOrder.length===3){

ranTrial=Math.floor(Math.random()*3);
trial=trialOrder[ranTrial];
trialOrder.splice(ranTrial,1);

ranNumber=Math.floor(Math.random()*6);
ranDL=randoms[ranNumber];
randoms.splice(ranNumber,1);
ranNumber=Math.floor(Math.random()*5);
ranDR=randoms[ranNumber];
randoms.splice(ranNumber,1);

resultString =resultString+"T"+trial+ranDL+ranDR;
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
} 

else if (trialOrder.length===2){

ranTrial=Math.floor(Math.random()*2);
trial=trialOrder[ranTrial];
trialOrder.splice(ranTrial,1);

ranNumber=Math.floor(Math.random()*4);
ranDL=randoms[ranNumber];
randoms.splice(ranNumber,1);
ranNumber=Math.floor(Math.random()*3);
ranDR=randoms[ranNumber];
randoms.splice(ranNumber,1);

resultString =resultString+"T"+trial+ranDL+ranDR;
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
}

else {
block++;
trial=trialOrder[0];
trialOrder=[1,2,3,4];

ranNumber=Math.floor(Math.random()*2);
ranDL=randoms[ranNumber];
randoms.splice(ranNumber,1);
ranNumber=0;
ranDR=randoms[ranNumber];

resultString =resultString+"T"+trial+ranDL+ranDR;
Qualtrics.SurveyEngine.setEmbeddedData("result",resultString);
}
document.getElementById(DR[ranDR]).className = "";
document.getElementById(DL[ranDL]).className = "";
document.getElementById(DR[ranDR]).setAttribute("onclick","drawFromDeckR()");
document.getElementById(DL[ranDL]).setAttribute("onclick","drawFromDeckL()");
document.getElementById(DL[ranDL]).style.visibility="visible";
document.getElementById(DR[ranDR]).style.visibility="visible";

}
else {
document.getElementById("roundNumber").innerHTML = "You finished the rounds!"
document.getElementById("topInstructions").innerHTML = 'Click "continue" to answer a set of questionnaires';
document.getElementById(DR[ranDR]).className = "Right";
document.getElementById(DL[ranDL]).className = "Left";
document.getElementById(R[ranR]).className = "Right";
document.getElementById(L[ranL]).className = "Left";
document.getElementById("bottomButton").style.display="none";
$('NextButton').setAttribute("style", "display:''");

}
}

